/*
 * This file is part of Dependency-Track.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (c) Steve Springett. All Rights Reserved.
 */
package org.dependencytrack.tasks;

import alpine.event.framework.Event;
import alpine.event.framework.Subscriber;
import alpine.logging.Logger;
import org.dependencytrack.event.AbstractVulnerabilityManagementUploadEvent;
import org.dependencytrack.integrations.FindingUploader;
import org.dependencytrack.integrations.PortfolioFindingUploader;
import org.dependencytrack.integrations.ProjectFindingUploader;
import org.dependencytrack.model.Finding;
import org.dependencytrack.model.Project;
import org.dependencytrack.persistence.QueryManager;
import java.io.InputStream;
import java.util.List;

public abstract class VulnerabilityManagementUploadTask implements Subscriber {

    private static final Logger LOGGER = Logger.getLogger(VulnerabilityManagementUploadTask.class);

    protected void inform(final Event e, final FindingUploader findingsUploader) {
        if (e instanceof AbstractVulnerabilityManagementUploadEvent) {
            try (QueryManager qm = new QueryManager()) {
                findingsUploader.setQueryManager(qm);
                if (findingsUploader.isEnabled()) {
                    if (findingsUploader instanceof ProjectFindingUploader) {
                        for (final Project project : qm.getAllProjects()) {
                            processProjectFindings((ProjectFindingUploader) findingsUploader, qm, project);
                        }
                    } else if (findingsUploader instanceof PortfolioFindingUploader) {
                        final PortfolioFindingUploader uploader = (PortfolioFindingUploader) findingsUploader;
                        final InputStream payload = uploader.process();
                        uploader.upload(payload);
                    }
                }
            } catch (Exception ex) {
                LOGGER.error(ex.getMessage());
            }
        }
    }

    private void processProjectFindings(final ProjectFindingUploader uploader, final QueryManager qm, final Project project) {
        if (uploader.isProjectConfigured(project)) {
            LOGGER.debug("Initializing integration point: " + uploader.name() + " for project: " + project.getUuid());
            final List<Finding> findings = qm.getFindings(project);
            final InputStream payload = uploader.process(project, findings);
            LOGGER.debug("Uploading findings to " + uploader.name() + " for project: " + project.getUuid());
            uploader.upload(project, payload);
        }
    }
}
